package com.feilong.edgeAI.remind.alarm;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import com.feilong.edgeAI.model.AIUIMsg;
import com.feilong.edgeAI.remind.room.bean.RemindBean;
import com.feilong.edgeAI.remind.room.db.RemindDatabase;

import org.greenrobot.eventbus.EventBus;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class AlarmReceiver extends BroadcastReceiver{

    private String strTts = "";

    @Override
    public void onReceive(Context context, Intent intent) {

        /* 处理提醒 */
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");  /***/
        String formattedDate = sdf.format(date);

        List<RemindBean> remindBeans = RemindDatabase.getDefault(context).getRemindDao().getRemindAll();
        for (int i = 0; i < remindBeans.size(); i++) {
            RemindBean remindBean = remindBeans.get(i);
            String time = remindBean.getHour();

            String[] split = time.split(":");
            if (split.length >1){
                time = split[0]+":"+split[1];
            }

            if (time.equals(formattedDate)){
                strTts = remindBean.getMessage();
                EventBus.getDefault().post(new AIUIMsg("主人，提醒时间到:"+strTts));
            }
        }

    }

}
